<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta http-equiv="Access-Control-Allow-Origin" content="*">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">

    <link rel="stylesheet" href="/layui/css/layui.css">
    <link  href="/hg-layui-admin-ui/css/font.css">
    <script src="/js/jquery-1.8.3.min.js"></script>
    <script src="/layui/layui.js"></script>


    <style>

    </style>
</head>

<body class="layui-body-content">
<div class="layui-fluid">
    <form class="layui-form" id="cxform" action="" style="margin: 2% 0% 0% 0%;">
        <div class="layui-colla-item" style="margin-left: 2%;margin-bottom: 2%;">
            <div class="layui-inline">
                <label class="layui-form-label" style="margin-left: -50px"><span >学生学号</span></label>
                <div  class="layui-input-inline">
                    <input type="text" id="xsxh" name="xsxh" class="layui-input" placeholder="学生学号" >
                </div>
            </div>

            <div class="layui-inline">
                <label class="layui-form-label" ><span >学生姓名</span></label>
                <div class="layui-input-inline">
                    <input type="text"   name="xsxm" id="xsxm" class="layui-input" placeholder="学生姓名" >
                </div>
            </div>
            <div class="layui-input-inline" style="margin-left:50px;">
                <button class="layui-btn layui-btn-normal layui-bg-green layui-btn-sm" id="cxbtn" type="button" lay-submit="" lay-filter="cxbtn">查询</button>
                <button class="layui-btn layui-btn-normal layui-hide" id="xzbtn" type="button" lay-submit="" lay-filter="xzbtn">新增</button>
            </div>
        </div>

        <table  class="layui-hide" id="test" lay-filter="test"></table>

    </form>
    <script type="text/html" id="bar">
        <a class="layui-btn layui-btn-xs layui-bg-orange" lay-event="xq">详情</a>
        <!--<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
    </script>

    <div id="addyhdiv" style="display: none;border-radius: 6px">
        <form class="layui-form" id="formqrmain" action="" style="margin-top: 50px;margin-left:4%;">

            <input type="hidden" id="id"  name="id">

            <!-- 新增  -->
            <div class="layui-form-item" >
                <label class="layui-form-label" style="width: 80px;"><span  >标题</span><span style="color: red">*</span></label>
                <div class="layui-input-inline">
                    <input type="text" id="title" name="title"  style="width: 270px;" placeholder="标题" class="layui-input qyzzlname" >
                </div>

                <label class="layui-form-label" style="width: 170px"><span >必修课学分</span><span style="color: red">*</span></label>
                <div class="layui-input-inline">
                    <input type="text" id="bxkxf"   name="bxkxf" style="width: 270px;"  placeholder="必修课学分"  class="layui-input" />
                </div>
            </div>

            <div class="layui-form-item"  style="margin-top: 25px;">
                <label class="layui-form-label" style="width: 80px;"><span  >限修课学分</span><span style="color: red">*</span></label>
                <div class="layui-input-inline">
                    <input type="text" id="xxkxf" name="xxkxf"  style="width: 270px;" placeholder="限选课学分" class="layui-input qyzzlname" >
                </div>

                <label class="layui-form-label" style="width: 170px"><span id="qyzzlmksjorfr">任选课学分</span><span style="color: red">*</span></label>
                <div class="layui-input-inline">
                    <input type="text" id="rxkxf"   name="rxkxf" style="width: 270px;"  placeholder="任选课学分"  class="layui-input" />
                </div>
            </div>


            <div class="layui-form-item" style="margin-top: 25px;">

                <label class="layui-form-label" style="width: 80px;"><span id="qyzzlsfzhorgddh">公选课学分</span><span style="color: red">*</span></label>
                <div class="layui-input-inline">
                    <input type="text" id="gxkxf" name="gxkxf" style="width: 270px;"  placeholder="公选课学分" class="layui-input" >
                    <!-- lay-verify="required"  -->
                </div>

                <div class=""  id="qyzzllxdz" style="margin-top: 25px;">
                    <label class="layui-form-label" style="width: 170px;">学生编号<span style="color: red">*</span></label>
                    <div class="layui-input-inline" style="width: 270px;" >
                        <select id="xsbh"  name="xsbh"  lay-filter="xsxhs">
                            <option value=" ">请选择学生编号</option>
                        </select>
                    </div>
                </div>
            </div>


            <div class="layui-form-item" style="margin-top: 25px;">
                <label class="layui-form-label" style="width: 80px;">登记日期</label>
                <div class="layui-input-inline">
                    <input type="text" id="djrq"  name="djrq" placeholder="登记日期" style="width: 270px;"  class="layui-input"  >
                </div>
                <label class="layui-form-label" style="width:170px;"><span >学生姓名</span><span style="color: red">*</span></label>
                <div class="layui-input-inline">
                    <input type="text" id="xsxms" name="xsxms"   readonly  style="width: 270px;"   placeholder="学生姓名" class="layui-input" >
                    <!-- lay-verify="required"  -->
                </div>
            </div>

        </form>
    </div>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button  type="button" class="layui-btn layui-btn-sm layui-bg-red" lay-event="getCheckData">批量删除</button>
        </div>
    </script>
</div>
</body>
<script>



    $(function() {
        pdbtn();
        getAllInfo();
        getAllStudents();
    });

    $("#cxbtn").click(function(){
        getAllInfo();
    });

    function pdbtn(){
        $.ajax({
            type:"POST",
            url:"/admin/user/getUserInfo",
            dataType:"json",
            async : true, //是否异步
            success:function (data) {
                if(data.roleId==3){
                    // $("#xzbtn").hide();
                    // $("#bar").html("<a class=\"layui-btn layui-btn-xs layui-bg-orange\" lay-event=\"xq\">详情</a> ");
                    // $("#toolbarDemo").html("");
                    //查询学生的成绩是否没达到要求
                    $.ajax({
                        type:"POST",
                        url:"/admin/rcpxfa/getAllRcpxfa",
                        dataType:"json",
                        data:{zy:""},
                        async : true, //是否异步
                        success:function (data) {
                            //获得方案的标准分  拿来与自己的比较
                            var datas = data.rows;
                            var mydata = getMyData();
                            //进行比较
                            layui.use('layer',function(){
                                var layer = layui.layer;
                                if(parseFloat(mydata[0].bxkxf)<parseFloat(datas[0].bxkxfyq)){
                                    alert("必修课学分未达标");
                                }
                                if(parseFloat(mydata[0].xxkxf)<parseFloat(datas[0].xxkxfyq)){
                                    alert("限选课学分未达标");
                                }
                                if(parseFloat(mydata[0].rxkxf)<parseFloat(datas[0].rxkxfyq)){
                                    alert("任选课学分未达标");
                                }
                                if(parseFloat(mydata[0].gxkxf)<parseFloat(datas[0].gxkxfyq)){
                                    alert("公选课学分未达标");
                                }
                            });


                        },
                        error: function (data,type, err) {
                            console.log("ajax错误类型："+type);
                            console.log(err);
                            layer.msg('删除失败！');
                        }
                    });




                }
            },
            error: function (data,type, err) {
                console.log("ajax错误类型："+type);
                console.log(err);
                layer.msg('删除失败！');
            }
        });
    }

    function getMyData() {
        var datas;
        jQuery.ajax({
            type : 'POST',
            url : 'getAllCredit',
            async : false,
            data : {},
            success: function(result){
                datas = result.rows;
            },
            error: function () {
                alert("获取单位信息失败:");
            }
        });
        return datas;
    }


    function getAllInfo(){
        var xsxh =  $("#xsxh").val();
        var xsxm =  $("#xsxm").val();
        $.ajax({
            type: "POST",
            url: "getAllCredit",
            data:{number:xsxh,name:xsxm},
            dataType: "json",
            success: function (data) {
                layui.use('table', function(){
                    var table = layui.table;
                    table.render({
                        elem: '#test',
                        data:data.rows,
                        height:518,
                        cols: [
                            [
                                {field:'id', width:180, title: 'ID', hide:true ,sort: true}
                                ,{field:'xsid', width:180, title: 'ID1', hide:true ,sort: true}
                                ,{field:'title', width:230, title: '标题', sort: true}
                                ,{field:'bxkxf', width:150, title: '必修课学分', sort: true}
                                ,{field:'xxkxf', width:150,title: '限选课学分', sort: true}
                                ,{field:'rxkxf', width:150, title: '任选课学分', sort: true}
                                ,{field:'gxkxf', width:150, title: '公选课学分', sort: true}
                                ,{field:'xsxh', width:220, title: '学生学号', sort: true}
                                ,{field:'xsxm', width:180, title: '学生姓名', sort: true}
                                ,{fixed: 'right', width:160, align:'center', toolbar: '#bar'}
                            ]
                        ]
                        ,page: true
                    });

                });


            },
            error: function (data, type, err) {
                console.log("ajax错误类型：" + type);
                console.log(err);
                layer.msg('删除失败！');
            }
        });
    }

    function getAllStudents(){
        $.ajax({
            type:"POST",
            url:"/admin/student/getAllStudents",
            data:{number:"",name:"",sex:"",rxnj:""},
            dataType:"json",
            async : false, //是否异步
            success:function (data) {
                var str = "<option value=''>请选择学生编号</option> ";
                if(data.rows.length>0){
                    var datas = data.rows;
                    for (let i = 0; i < datas.length ; i++) {
                        str += "<option value="+datas[i].id+">"+datas[i].number+"</option>";
                    }
                }else{
                    str = "<option value=''>暂无学生信息</option> ";
                }
                $("#xsbh").html(str);
                layui.use('form',function(){
                    var form = layui.form;
                    form.render();
                });
            },
            error: function (data,type, err) {
                console.log("ajax错误类型："+type);
                console.log(err);
                layer.msg('删除失败！');
            }
        });

    };



    layui.use(['table','form','laydate'], function() {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        var laydate = layui.laydate;

        form.on('select(xsxhs)',function(data){
            var id = data.value;
            $.ajax({
                type:"POST",
                url:"/admin/student/getStudent",
                dataType:"json",
                data:{id:id},
                async : true, //是否异步
                success:function (data) {
                    if(data!=''&&data!=null){
                        $("#xsxms").val(data.name);
                    }else{
                        $("#xsxms").val("");
                    }
                },
                error: function (data,type, err) {
                    console.log("ajax错误类型："+type);
                    console.log(err);
                    layer.msg('删除失败！');
                }
            });

        });



        //执行一个laydate实例
        laydate.render({
            elem: '#djrq' //指定元素
        });
        /*新增学生*/
        form.on('submit(xzbtn)',function () {
            $("#formqrmain input").prop("readonly",false);
            var addlayer = layer.open({
                type : 1,
                title : "新增学生学分",
                btn:['确定','取消'],
                area : [ '52%', '62%' ],
                offset:'10%',
                shadeClose : false, //点击遮罩关闭
                content : $('#addyhdiv'),
                success : function() {
                    document.getElementById("formqrmain").reset();
                    layui.form.render();
                    return;
                },
                yes:function(index,layero){
                    var title = $("#title").val();
                    var bxkxf = $("#bxkxf").val();
                    var xxkxf = $("#xxkxf").val();
                    var rxkxf = $("#rxkxf").val();
                    var gxkxf =  $("#gxkxf").val();
                    var xsbh =  $("#xsbh").val();
                    var djrq =  $("#djrq").val();
                    var xsxms =  $("#xsxms").val();

                    if(title == null || title == ""){
                        layer.msg("标题不能为空！");
                        return;
                    }
                    if(bxkxf == null || bxkxf == ""){
                        layer.msg("必修课学分不能为空！");
                        return;
                    }
                    if(rxkxf == null || rxkxf == ""){
                        layer.msg("任选课学分不能为空！");
                        return;
                    }

                    if(gxkxf == null || gxkxf == ""){
                        layer.msg("公选课学分不能为空！");
                        return;
                    }

                    if(xsbh == null || xsbh == ""){
                        layer.msg("学生编号不能为空！");
                        return;
                    }

                    if(xsxms == null || xsxms == ""){
                        layer.msg("学生姓名不能为空！");
                        return;
                    }

                    console.log("打印值");
                    var credit ={
                        title : title,
                        bxkxf : bxkxf,
                        xxkxf : xxkxf,
                        rxkxf : rxkxf,
                        gxkxf : gxkxf,
                        name : xsxms,
                        number : xsbh,
                        djrq : djrq,
                    };
                    console.log(credit);
                    $.ajax({
                        type:"POST",
                        url:"add",
                        data:credit,
                        success:function (data) {
                            layer.msg("增加成功");
                            layer.close(addlayer);
                            getAllInfo();
                        },
                        error: function (data,type, err) {
                            console.log("ajax错误类型："+type);
                            console.log(err);

                            layer.alert("增加失败", {
                                    icon: 6
                                },
                                function () {
                                });
                        }
                    });

                },
                cancel : function() {
                    return;
                },
                end : function() {
                    //$('#main').css("display", "none");
                }
            });


        })


        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var data = checkStatus.data;
            if(data.length==0){
                layer.msg("请选择数据！");
                return false;
            }
            layer.confirm('是否批量删除',{
                title:"提示信息"
            }, function(index) {
                var str ="";

                for (let i = 0; i <data.length ; i++) {
                    if(i==data.length-1){
                        str += " '"+data[i].id+"' ";
                    }else{
                        str += " '"+data[i].id+"', ";
                    }
                }
                console.log(str);
                $.ajax({
                    type:"POST",
                    url:"deletes",
                    data:{"ids":str},
                    dataType:"json",
                    success:function (data) {
                        if(data === true){//删除成功：移除删除行
                            layer.msg('提示：删除成功！');
                            getAllInfo();
                        }else{//删除失败
                            layer.msg('提示：删除失败！');
                        };
                    },
                    error: function (data,type, err) {
                        console.log("ajax错误类型："+type);
                        console.log(err);
                        hg.msg('删除失败！');
                    }
                });

                layer.close(index);
            });

            return false;
        });



        table.on('tool(test)', function(obj){
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event;
            if(layEvent === 'edit'){
                $("#formqrmain input").prop("readonly",false);
                var addlayer = layer.open({
                    type : 1,
                    title : "编辑信息",
                    btn:['确定','取消'],
                    area : [ '51%', '62%' ],
                    offset:'10%',
                    shadeClose : false, //点击遮罩关闭
                    content : $('#addyhdiv'),
                    success : function() {
                        $("#title").val(data.title);
                        $("#bxkxf").val(data.bxkxf);
                        $("#xxkxf").val(data.xxkxf);
                        $("#rxkxf").val(data.rxkxf);
                        $("#gxkxf").val(data.gxkxf);
                        $("#xsxms").val(data.xsxm);
                        $("#xsbh").val(data.xsid);
                        console.log("--")
                        console.log(data)
                        $("#djrq").val(data.djrq);
                        $("#id").val(data.id);
                        layui.form.render();
                        return;
                    },
                    yes:function(index,layero){
                        var id = $("#id").val();
                        var title = $("#title").val();
                        var bxkxf = $("#bxkxf").val();
                        var xxkxf = $("#xxkxf").val();
                        var rxkxf = $("#rxkxf").val();
                        var gxkxf =  $("#gxkxf").val();
                        var xsbh =  $("#xsbh").val();
                        var djrq =  $("#djrq").val();
                        var xsxms =  $("#xsxms").val();

                        if(title == null || title == ""){
                            layer.msg("标题不能为空！");
                            return;
                        }
                        if(bxkxf == null || bxkxf == ""){
                            layer.msg("必修课学分不能为空！");
                            return;
                        }
                        if(rxkxf == null || rxkxf == ""){
                            layer.msg("任选课学分不能为空！");
                            return;
                        }

                        if(gxkxf == null || gxkxf == ""){
                            layer.msg("公选课学分不能为空！");
                            return;
                        }

                        if(xsbh == null || xsbh == ""){
                            layer.msg("学生编号不能为空！");
                            return;
                        }

                        if(xsxms == null || xsxms == ""){
                            layer.msg("学生姓名不能为空！");
                            return;
                        }

                        console.log("打印值");
                        var credit ={
                            id:id,
                            title : title,
                            bxkxf : bxkxf,
                            xxkxf : xxkxf,
                            rxkxf : rxkxf,
                            gxkxf : gxkxf,
                            name : xsxms,
                            number : xsbh,
                            djrq : djrq,
                        };
                        console.log(credit);
                        $.ajax({
                            type:"POST",
                            url:"edit",
                            data:credit,
                            success:function (data) {
                                layer.msg("修改成功");
                                layer.close(addlayer);
                                getAllInfo();
                            },
                            error: function (data,type, err) {
                                console.log("ajax错误类型："+type);
                                console.log(err);

                                layer.alert("修改失败", {
                                        icon: 6
                                    },
                                    function () {
                                    });
                            }
                        });

                    },
                    cancel : function() {
                        return;
                    },
                    end : function() {
                        //$('#main').css("display", "none");
                    }
                });
            }else if(layEvent === 'del'){
                layer.confirm('是否删除',{
                    title:"提示信息"
                }, function(index) {
                    $.ajax({
                        type:"POST",
                        url:"delete",
                        data:{"id":data.id},
                        dataType:"json",
                        success:function (data) {
                            if(data === true){//删除成功：移除删除行
                                layer.msg('提示：删除成功！');
                                getAllInfo();
                            }else{//删除失败
                                layer.msg('提示：删除失败！');
                            };
                        },
                        error: function (data,type, err) {
                            console.log("ajax错误类型："+type);
                            console.log(err);
                            hg.msg('删除失败！');
                        }
                    });

                    layer.close(index);
                });

            }else if(layEvent === 'xq'){
                var addlayer = layer.open({
                    type : 1,
                    title : "详情",
                    btn:['取消'],
                    area : [ '51%', '62%' ],
                    offset:'10%',
                    shadeClose : false, //点击遮罩关闭
                    content : $('#addyhdiv'),
                    success : function() {
                        $("#formqrmain input").attr("readonly","readonly");
                        $("#title").val(data.title);
                        $("#bxkxf").val(data.bxkxf);
                        $("#xxkxf").val(data.xxkxf);
                        $("#rxkxf").val(data.rxkxf);
                        $("#gxkxf").val(data.gxkxf);
                        $("#xsxms").val(data.xsxm);
                        $("#xsbh").val(data.xsid);
                        console.log("--")
                        console.log(data)
                        $("#djrq").val(data.djrq);
                        $("#id").val(data.id);
                        layui.form.render();
                        return;
                    },
                    yes:function(index,layero){
                        layer.close(index);
                    },
                    cancel : function() {
                        return;
                    },
                    end : function() {
                        //$('#main').css("display", "none");
                    }
                });


            }



        })

    })




</script>

</html>